var PeopleMedia = PeopleMedia || {};
PeopleMedia.NotificationsToolbar = new function () {
    var $toolBar = $jq(document.getElementById('NotificationsToolbar'));

    if (PMChat.ChatEnabled) {
        if (this.chatTimerUpdate == null) {
            this.chatTimerUpdate = setInterval(function () {
                PeopleMedia.NotificationsToolbar.SetOnlineCount();
            }, 60000);
        }
    }

    $toolBar.find('div.btnRecentNotifications[title]').click(function () {
        $toolBar.find('div.tbRecentNotifications').animate({ bottom: '-37px' }, 150);
        $toolBar.find('#recentNotifications').html('<img src="' + PeopleMedia.SiteImagePath + '/images/upgrade/waitwheel.gif" alt="" />');
        $toolBar.find('#recentNotifications').load('/v3/notification/GetToolbarNotifications');
    });
    $toolBar.find('div.tbRecentNotifications > div:last-child').click(function () {
        $toolBar.find('div.tbRecentNotifications').animate({ bottom: '-555px' }, 150);
    });
    $toolBar.find('.tbRecentNotifications > div:first-child > div:first-child img').click(function () {
        $toolBar.find('div.tbRecentNotifications').animate({ bottom: '-555px' }, 150);
    });
    $jq('body').click(function (e) {
        if ($jq(e.target).is('#NotificationsToolbar *'))
            return;
        $toolBar.find('div.tbRecentNotifications').animate({ bottom: '-555px' }, 150);
        $toolBar.find('div.tbChat').animate({ bottom: '-565px' }, 150);
    });

    $toolBar.find('div.tbRecentNotifications > div:last-child').addClass('tbLast');
    $toolBar.find('div.tbChat > div:last-child').addClass('tbLast');

    $toolBar.find('div.btnNextSteps').click(function () { $jq('div.tbNextSteps').animate({ bottom: '-37px' }, 150); });
    $toolBar.find('div.tbNextSteps div:last-child').click(function () { $jq('div.tbNextSteps').animate({ bottom: '-151px' }, 150); });

    $toolBar.find('div.btnNextSteps[data-cta]').click(function () { location.href = $toolBar.find('div.tbNextStepsDisplay a').attr('href'); });
    $toolBar.find('div.tbNextStepsDisplay').click(function () { location.href = $toolBar.find('div.tbNextStepsDisplay a').attr('href'); });

    $toolBar.find('div.btnChat span').click(function () {
        if (PMChat.ChatEnabled) {
            clearInterval(PeopleMedia.NotificationsToolbar.chatTimerUpdate);
            showChatList();
            $toolBar.find('#chatMemberList').html('<img src="' + PeopleMedia.SiteImagePath + '/images/upgrade/waitwheel.gif" alt="" />');
            $toolBar.find('#chatMemberList').append('<div class="chatMemberSuperList"></div>');
            PeopleMedia.NotificationsToolbar.SetOnlineCount();
        }
        else {
            PMChat.SetOnlineStatus(1, PeopleMedia.InternalHeader.toggleChatComplete);
        }
    });
    var hideChatList = function() {
        if (PeopleMedia && PeopleMedia.ChatPane && PeopleMedia.ChatPane.IsEnabled == true) {
            PeopleMedia.ChatPane.ToggleChat();
        } else {
            $jq('div.tbChat').animate({ bottom: '-565px' }, 150);
        }
    };
    var showChatList = function() {
        if (PeopleMedia && PeopleMedia.ChatPane && PeopleMedia.ChatPane.IsEnabled == true) {
            PeopleMedia.ChatPane.ToggleChat();
        } else {
            $jq('div.tbChat').animate({ bottom: '-37px' }, 150);
        }
    };
    $toolBar.find('div.tbChat > div:last-child').click(hideChatList);

    $toolBar.find('.tbChat > div:first-child > div:first-child img').click(hideChatList);

    $toolBar.find('div.btnChat > img:first-child').click(PeopleMedia.InternalHeader.toggleChat);

    this.SetOnlineCount = function () {
        if (PMChat.ChatEnabled) {
            $toolBar.find('#chatMemberList').load('/v3/connect/GetToolbarFavorites', function () {
                var online = $toolBar.find('#chatMemberList').find('div[data-online-count]').attr('data-online-count');
                $toolBar.find('span[data-online-label]').html(online);
            });
        }
    };

    var contactHTML = $toolBar.find('.chatMemberSuperList');
    var contactLength = 0;

    PMChat.On("getcontacts", function (e, contacts) {
        contactLength = contacts.length;

        if (contacts.length > 0) {
            for (var c in contacts) {
                var name = contacts[c].NickName;
                contactHTML.append('<div>' + name + '</div>');
            }
        }

        PeopleMedia.NotificationsToolbar.SetFavorites;
    });

    this.SetFavorites = function () {
        $toolBar.find('#chatMemberList').html('<div class="buddyList" data-online-count="' + contactLength + '">' + contactHTML + '</div>');
    };

};
$jq(document).ready(PeopleMedia.NotificationsToolbar.SetOnlineCount);

